var AccountSearchBox = {

    setupFormValidation: function () {
        this.searchFormValidator = $('#form_search_condition').validate({
            rules: {
                'number:string': {
                    maxlength: 10,
                    pattern: '[A-Za-z0-9]+'
                },
                'name:string': {
                    maxlength: 50
                },
                'idNumber:string': {
                    maxlength: 8,
                    pattern: '[\\d|X]+'
                }
            },
            messages: {
                'number:string': {
                    maxlength: '最多 10 个字符',
                    pattern: '只能输入数字与字母'
                },
                'name:string': {
                    maxlength: '最多 50 个字符'
                },
                'idNumber:string': {
                    maxlength: '最多 18 个字符',
                    pattern: '只能输入数字和大写字母 X'
                }
            }
        });
    },

    resetForm: function () {
        this.searchFormValidator.resetForm();
        $('#form_search_condition')[0].reset();
        $('#form_search_condition input.uk-form-danger').removeClass('uk-form-danger');
    },

    doSearch: function (beforeSearch, done, fail, always, onlyActiveAccount) {
        var form = $('#form_search_condition');
        if (form.valid()) {
            var searchCondition = form.serializeJSON({skipFalsyValuesForTypes: ['string', 'number']});
            if (searchCondition.name == null && searchCondition.number == null && searchCondition.idNumber == null) {
                UIkit.modal.alert("至少指定一个条件");
                return;
            }

            if (onlyActiveAccount != null && !onlyActiveAccount)
                searchCondition.onlyActiveAccount = false;

            accountList.clear();

            if (beforeSearch != null) {
                beforeSearch();
            }
            $.ajax({
                url: "/api/admin/search_account",
                data: searchCondition,
                method: 'GET',
                contentType: 'application/json',
                dataType: 'json'
            })
                .done(function (data) {
                    if (done != null) {
                        done(data);
                    }
                })
                .fail(function (jqXHR) {
                    if (fail != null) {
                        fail(jqXHR);
                    }
                })
                .always(function () {
                    if (always != null) {
                        always();
                    }
                });
        }
    }
};